package com.aigou.admin.controller;

import com.aigou.admin.entity.OrderProduct;
import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.ResponseEntity;
import com.aigou.admin.service.OrderProductService;
import com.aigou.admin.service.impl.OrderProductServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/orderProducts")
public class OrderProductController extends BaseServlet{

    private OrderProductService orderProductService = new OrderProductServiceImpl();

    //http://localhost:8080/orderProduct?m=findAll
    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<OrderProduct> orderProducts = orderProductService.getAllOrderProducts();
        ResponseEntity.ok(resp,orderProducts);
    }

    //http://localhost:8080/orderProduct?m=findById&id=31
    public void findById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        OrderProduct result = orderProductService.getOrderProductById(id);
        ResponseEntity.ok(resp,result);
    }

    //http://localhost:8080/orderProduct?m=save&id=301&order_id=1&product_id=1&product_num=2
    public void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String Id = req.getParameter("id");
        String orderId = req.getParameter("order_id");
        String productId = req.getParameter("product_id");
        int productNum = Integer.parseInt(req.getParameter("product_num"));
        OrderProduct orderProduct = new OrderProduct(Id, orderId, productId, productNum);

        int result = orderProductService.addOrderProduct(orderProduct);
        ResponseEntity.ok(resp,result);
    }

    //http://localhost:8080/orderProduct?m=delete&id=301
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        int result = orderProductService.deleteOrderProduct(id);
        ResponseEntity.ok(resp,result);
    }

    //http://localhost:8080/orderProduct?m=update&id=301&order_id=1&product_id=1&product_num=2
    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String orderId = req.getParameter("order_id");
        String productId = req.getParameter("product_id");
        int productNum = Integer.parseInt(req.getParameter("product_num"));
        OrderProduct orderProduct = new OrderProduct(id, orderId, productId, productNum);
        int result = orderProductService.updateOrderProduct(orderProduct);
        ResponseEntity.ok(resp,result);
    }

    //http://localhost:8080/orderProduct?m=page&current=1&pageSize=5
    public void page(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String current = req.getParameter("current");
        String pageSize = req.getParameter("pageSize");
        // 查询用户
        PageEntity<OrderProduct> pageEntity = orderProductService.findOrderProductPage(Integer.valueOf(current), Integer.valueOf(pageSize));
        // 转换为JSON,通过流发送给客户端
        ResponseEntity.ok(resp,pageEntity);
    }
}
